/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.yuhanginfo.modules.sys.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yuhanginfo.common.persistence.Page;
import com.yuhanginfo.common.web.BaseController;
import com.yuhanginfo.common.utils.StringUtils;
import com.yuhanginfo.modules.sys.entity.Parts;
import com.yuhanginfo.modules.sys.entity.PartsChange;
import com.yuhanginfo.modules.sys.service.PartsChangeService;
import com.yuhanginfo.modules.sys.service.PartsService;

/**
 * 配件互换
 * @author yinhao
 * @version 2017-10-12
 */
@Controller
@RequestMapping(value = "${adminPath}/sys/partschange")
public class PartsChangeController extends BaseController {

	private static final  Logger logger = LoggerFactory.getLogger(PartsChangeController.class);
	
	@Autowired
	private PartsChangeService partsChangeService;
	
	@Autowired
	private PartsService partsService;
	
	private static final  String RESULTCODE = "resultCode";
	@ModelAttribute
	public PartsChange get(@RequestParam(required=false) String id) {
		PartsChange entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = partsChangeService.get(id);
		}
		if (entity == null){
			entity = new PartsChange();
		}
		return entity;
	}
	
	@RequiresPermissions("sys:partschange:view")
	@RequestMapping(value = {"list", ""})
	public String list(PartsChange partsChange, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<PartsChange> page = partsChangeService.findPage(new Page<PartsChange>(request, response), partsChange); 
		model.addAttribute("page", page);
		return "modules/sys/partschange/partschangeList";
	}
	
	@RequiresPermissions("sys:partschange:edit")
	@RequestMapping(value = "form")
	@ResponseBody
	public Map<String,Object> form(Parts parts) {
		Map<String,Object> resultMap = new HashMap<>();
		try{
			if(null == parts.getCode() || "".equals(parts.getCode())){
				resultMap.put(RESULTCODE, 0);
				resultMap.put("resultMessage", "请输入配件号");
				return resultMap;
			}
			resultMap.put("list",partsService.findList(parts));
			resultMap.put(RESULTCODE, 1);
		}catch(Exception ex){
			logger.info("form",ex);
			resultMap.put(RESULTCODE, 0);
		}
		return resultMap;
	}

	
	@RequiresPermissions("sys:partschange:edit")
	@RequestMapping(value = "save")
	public String save(String groupId,String[] partsCode, Model model, HttpServletRequest request, HttpServletResponse response) {
		try {
			partsChangeService.save(groupId,partsCode);
		} catch (Exception ex) {
			logger.info("form",ex);
		}
		return this.list(new PartsChange(), request, response, model);
	}

	@RequiresPermissions("sys:partschange:edit")
	@RequestMapping(value = "toModify")
	@ResponseBody
	public Map<String,Object> toModify(PartsChange partsChange, Model model,HttpServletRequest request, HttpServletResponse response) {
		Map<String,Object> map = new HashMap<>();
		try {
			map.put("list",partsChangeService.findByGroupId(partsChange));
		} catch (Exception ex) {
			logger.info("form",ex);
		}
		return map;
	}

	@RequiresPermissions("sys:partschange:edit")
	@RequestMapping(value = "delete")
	public String delete(String groupId, HttpServletRequest request, HttpServletResponse response, Model model) {
		partsChangeService.del(groupId);
		model.addAttribute("resultMessage", "删除成功");
		return this.list(new PartsChange(), request, response, model);
	}
	

}